
(function () {

	var globalobj={}
	$(window).load(function() {
		var topH = $("header").height()

		$(document).on("scroll", function() {
			var scrollTop=$(document).scrollTop()
			// console.log(scrollTop)
			if(scrollTop>topH){
				$("nav").css({
					position:"fixed",
					top:0,
				})
				$("section").css({
					marginTop:$("nav").height()
				})
				$(".content").animate({marginTop:"220"
				},100,"swing")
			}else{
				$("nav").css({
					position:"static"
				})
				$("section").css({
					marginTop:0
				})
				$(".content").animate({marginTop:"400"
				},100,"swing")
			}
		})
		//显示购物车列表
		$(".box").on("mouseenter",".navRight",function(){
			$(".shopping").slideDown(500)
		})
		$(".box").on("mouseleave",".navRight",function(){
			$(".shopping").slideUp(500)
		})
		$(".box").on("click",".imgcha",function(){
			$(".shopping").hide(400)
		})
		//点击结算跳转
		$(".box").on("click",".addshoppbtn",function () {
			location.href="./shoppingcar.html"
		})
	})
	//点击首页回到主页面
	$(".box").on("click",".shouye",function(){
		location.href="./aaa.html"
	})
	//显示二级菜单

	$(".navPop").on("mouseenter",".gofloor",function () {
		$(this).children(".List").eq(0).show()
	}).on("mouseleave",".gofloor",function () {
		$(this).children(".List").eq(0).hide()
	})

	$.ajax({
		url:"./../server/getgoodslist.php",
		dataType:"json",
		type:"post"
	}).then(function (res) {


		if(res.status==1){
			globalobj=res.data
			console.log(res.data)
			var totalprice=Number($(".addshopcar").text().replace("¥",""))
			res.data.forEach(function (el,index) {
				var $li =
					`<li class="hidegoodslist" data-tmpindex="${index}">
                    					<div class='smallshopimg'>
                    					<a href='#'><img src='${el.url}'></a>
                    					</div>
                    					<div class='shoptext'>
                    						<a href='#'>${el.name}</a>
                    						<a href='#' class="goodssize">尺寸:${el.size2}</a>
                    						<a href='javascript:;;' class='closebtn' data-total="${el.total}">删除</a>
                    					</div>
                    				</li>`
				$(".goodsListUL").append($li)
				totalprice+=Number(el.total)
			})
			$(".listnum").text(res.data.length)
			$(".addshopcar").text("¥"+totalprice+".00")
		}

	})

		//删除操作
	$(".shopping").on("click",".closebtn",function(){
		var self=this
		deleteGoods(self,".hidegoodslist")
	})

	function deleteGoods(self,parent){
		var singleobj=globalobj[$(self).parents(parent).data("tmpindex")]
		var hideTotal=Number($(self).data("total"))
		var totalprice=Number($(".addshopcar").text().replace("¥",""))
		console.log(hideTotal)
		var updateobj={
			cid:singleobj.id
		}
		layer.confirm("确定删除该商品吗",{
			icon:3,
			title:"删除提示",
			btn:["确定删除","取消"]
		},function (index) {
			$.ajax({
				url:"./../server/removegoods.php",
				data:updateobj,
				dataType: "json",
				type:"post"
			}).then(function (res) {

				if(res.status==1){
					layer.msg("删除成功")
					//删除页面显示元素,修改购物包数量和价格
					$(self).parents(parent).remove()
				$(".listnum").text(Number($(".listnum").text())-1)
                $(".addshopcar").text("¥"+(totalprice-hideTotal)+".00")
					
				}else {
					layer.msg("删除失败")
				}
				layer.close(index)
			})
		},function () {
		})
	}

})()